package apiwork.lcbmi.util.config;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Element;

import com.linkage.util.config.MemoryConfig;
import com.linkage.util.config.SystemConfig;
import com.linkage.util.xml.XMLProperties;

public class LcbmiMainConfig extends MemoryConfig{
	private Logger log=Logger.getLogger(LcbmiMainConfig.class);//zhaigx
	private static LcbmiMainConfig m_instance = null;
	private static XMLProperties xmlProperties = null;
	
	private LcbmiMainConfig(){}
	
	public static LcbmiMainConfig getInstance() {
		if (m_instance == null) {
			m_instance = new LcbmiMainConfig();
		}
		return m_instance;
	}
	
	synchronized public void reload() throws Exception {
		try{
			if(isReload() || xmlProperties == null){
				String xmlFile = SystemConfig.getInstance().getConfig("LcbmiMainConfig");
				log.info("LcbmiMainConfig="+xmlFile);
				xmlProperties = new XMLProperties(xmlFile);
			}
			setReload(false);
		}catch(Exception e){
			throw new Exception(e);
		}
	}
	
	public String getProperties(String name) throws Exception{
		try{
			reload();
			return xmlProperties.getElementValue(name);
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
    }
	/**
	 * 
	 * @param name
	 * @return
	 * @throws Exception
	 * @author zhaigx 66206
	 * @date 2011-8-28
	 */
	public List getElements(String name) throws Exception{
		try{
			reload();
			return xmlProperties.getElements(name);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
    }
	
	public Map getSubEleInfo(String name) throws Exception{
		try{
			HashMap map=new HashMap();
			reload();
			List list=xmlProperties.getElements(name);
			for (int i = 0; i < list.size(); i++) {
				Element element=(Element)list.get(i);
				map.put(element.getName(), xmlProperties.getAllSubElementInfo(element,true));
			}
			 
			return map;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
